﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Contract.SaleOut.Record;
using DataCenter.Data;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using Common.Contract;
using Common.Util;
using System.Data.Entity;

namespace DataCenter.Provider
{
    public class OrderProvider : GenericProvider, IOrderProvider
    {
        public List<OrderInfo> SearchOrders(out int totalRecord, string textSearch, string fromDate = null, string toDate = null, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<OrderInfo>();

            if (!string.IsNullOrWhiteSpace(textSearch))
            {
                querry = querry.Where(x => x.OrderNo.ToUpper().Equals(textSearch.ToUpper())
                     || x.BilltoCode.ToUpper().Contains(textSearch.ToUpper())
                     || x.BilltoName.ToUpper().Contains(textSearch.ToUpper())
                     || x.InvoiceNo.ToUpper().Equals(textSearch.ToUpper())
                     || x.Model.ToUpper().Contains(textSearch.ToUpper())
                     || x.SaleschannelName.ToUpper().Contains(textSearch.ToUpper())
                     || x.Salesperson.ToUpper().Contains(textSearch.ToUpper())
                     || x.ShippingMethod.ToUpper().Contains(textSearch.ToUpper())
                     || x.ShiptoCode.ToUpper().Equals(textSearch.ToUpper())
                     || x.ShiptoName.ToUpper().Contains(textSearch.ToUpper())
                     || x.City.ToUpper().Contains(textSearch.ToUpper())
                   );
            }
            if (!string.IsNullOrEmpty(fromDate))
            {
                var frDate = fromDate.ToDateTime();
                querry = querry.Where(x => x.RequestArrivalDate > frDate);
            }
            if (!string.IsNullOrEmpty(toDate))
            {
                var toDa = toDate.ToDateTime();
                querry = querry.Where(x => x.RequestArrivalDate < toDa);
            }
            querry = querry.OrderBy(x => x.OrderNo);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }
        public List<OrderInfo> SearchOrders(out int totalRecord, string shipcode, string billcode, string shipname, string city, DateTime? invoicedate, bool? transferFlag = null, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<OrderInfo>();

            if (!string.IsNullOrWhiteSpace(shipcode))
            {
                querry = querry.Where(x => x.ShiptoCode.ToUpper().Contains(shipcode.ToUpper()));
            }

            if (!string.IsNullOrWhiteSpace(billcode))
            {
                querry = querry.Where(x => x.BilltoCode.ToUpper().Contains(billcode.ToUpper()));
            }


            if (!string.IsNullOrWhiteSpace(shipname))
            {
                querry = querry.Where(x => x.ShiptoName.ToUpper().Contains(shipname.ToUpper()));
            }

            if (!string.IsNullOrWhiteSpace(city))
            {
                querry = querry.Where(x => x.City.ToUpper().Contains(city.ToUpper()));
            }

            if (invoicedate.HasValue)
            {
                querry = querry.Where(x => x.InvoiceDate == invoicedate);
            }
            if (transferFlag.HasValue)
            {
                string flag = transferFlag.Value.ToBooleanString();
                if (!transferFlag.Value) querry = querry.Where(x => x.SoTransferFlag.ToUpper() == flag);
                else querry = querry.Where(x => x.SoTransferFlag == null || x.SoTransferFlag.Length == 0 || x.SoTransferFlag.ToUpper() == flag);
            }
            querry = querry.OrderBy(x => x.OrderNo);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public OrderInfo Retrieve(string orderNumber, string invoiceNumber, string lineNumber)
        {
            var orderNo = (orderNumber ?? string.Empty).ToUpper();
            var inviceNo = (invoiceNumber ?? string.Empty).ToUpper();
            var lineNo = (lineNumber ?? string.Empty).ToUpper();
            return repository.FindOne<OrderInfo>(x => x.InvoiceNo.ToUpper() == inviceNo && x.OrderNo.ToUpper() == orderNo && x.LineNo.ToUpper() == lineNo);
        }

        public bool Add(OrderInfo obj)
        {
            try
            {
                repository.Orders.Add(obj);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(OrderInfo obj)
        {
            var updateRecord = Retrieve(obj.OrderNo, obj.InvoiceNo, obj.LineNo);
            try
            {
                updateRecord.AcceptanceCode = obj.AcceptanceCode;
                updateRecord.ArFlag = obj.ArFlag;
                updateRecord.AuCode = obj.AuCode;
                updateRecord.BilltoCode = obj.BilltoCode;
                updateRecord.BilltoName = obj.BilltoName;
                updateRecord.BookedDate = obj.BookedDate;
                updateRecord.CancelFlag = obj.CancelFlag;
                updateRecord.Carrier = obj.Carrier;
                updateRecord.City = obj.City;
                updateRecord.DeliveryNo = obj.DeliveryNo;
                updateRecord.InvoiceDate = obj.InvoiceDate;
                //updateRecord.InvoiceNo = obj.InvoiceNo;
                updateRecord.InvOrg = obj.InvOrg;
                //updateRecord.LineNo = obj.LineNo;
                updateRecord.LineStatus = obj.LineStatus;
                updateRecord.Model = obj.Model;
                updateRecord.ModelSuffix = obj.ModelSuffix;
                updateRecord.OrderAmt = obj.OrderAmt;
                //updateRecord.OrderNo = obj.OrderNo;
                updateRecord.OrderQty = obj.OrderQty;
                updateRecord.OrderTypeName = obj.OrderTypeName;
                updateRecord.PickNo = obj.PickNo;
                updateRecord.PickReleaseDate = obj.PickReleaseDate;
                updateRecord.PickSeq = obj.PickSeq;
                updateRecord.ProdDiv = obj.ProdDiv;
                updateRecord.ReleaseQty = obj.ReleaseQty;
                updateRecord.RequestArrivalDate = obj.RequestArrivalDate;
                updateRecord.RequestShippedDate = obj.RequestShippedDate;
                updateRecord.SaleschannelCode = obj.SaleschannelCode;
                updateRecord.SaleschannelName = obj.SaleschannelName;
                updateRecord.Salesperson = obj.Salesperson;
                updateRecord.ShippingMethod = obj.ShippingMethod;
                updateRecord.ShippingRemark = obj.ShippingRemark;
                updateRecord.ShiptoCode = obj.ShiptoCode;
                updateRecord.ShiptoName = obj.ShiptoName;
                updateRecord.Subinventory = obj.Subinventory;
                updateRecord.Suffix = obj.Suffix;
                updateRecord.UniqueId = obj.UniqueId;
                updateRecord.UnitSellPrice = obj.UnitSellPrice;
                updateRecord.SoTransferFlag = obj.SoTransferFlag;
                if (updateRecord.SoTransferFlag.IsYes()) updateRecord.SoTransferDate = DateTime.Now;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
